// 文档说明参见根目录下的readme.md 文件
export class baseModule {
    constructor(viewDom) {
        this.view = viewDom;
        this.config = {};
        this.watchEvents();

        //记录已经实例化的模块
        this.config.id = this.view.attr('id');
        funs.hasLoadModuleArray[this.config.id] = this;

        // 让模块的构造方法先执行；
        setTimeout(() => {
            this.afterInstall && this.afterInstall();
        }, 330);
    }
    watchEvents() {
        let _this = this;
        let actions = this;
        let view = this.view;
        let callAction = function (action, type, target, hit) {
            let fun = actions["_evt_" + action];
            if (fun) {
                return fun.call(_this, target, hit);
            }
            else {
                return true;
            }
        };
        view.on("click", function (e) {
            let type = e.type;
            let hit = e.target;
            let target = e.target;
            let nodeName = target.nodeName;
            let propagation = true;
            let root = this;
            if (type == "focusin" && nodeName != "INPUT" && nodeName != "TEXTARE") {
                return true;
            }
            if (type == "click" && (nodeName == "FORM" || nodeName == "SELECT" || nodeName == "OPTION" || nodeName == "TEXTARE" || nodeName == "INPUT" || (nodeName == "LABEL" && target.htmlFor))) {
                return true;
            }
            if (type == "change" && (nodeName == "FORM" || nodeName == "TEXTARE" || nodeName == "INPUT")) {
                return true;
            }
            while (target && target != root) {
                let actions_1 = target.getAttribute("evt");
                if (actions_1 && (!target.hasAttribute("disabled") || target.getAttribute("disabled") == 'false')) {
                    propagation = (callAction(actions_1, type, target, hit) ? true : false) && propagation;
                    return propagation;
                }
                if (target.nodeName == "FORM") {
                    return true;
                }
                target = target.parentNode;
            }
            return true;
        });
    }

    find(selector) {
        return this.view.find(selector);
    }
    // 页面模块返回事件
    _evt_goback() {

        let direction = this.config.direction;
        let panel = this.config.parent;
        let isUrl = this.config.isUrl;
        let type = this.config.type;

        // 如果是弹窗
        if(type == 'modal'){
            panel.find('.CenterModalContent').css({'transform':'scale(0,0)','opacity':'0'});
            this.afterUninstall && this.afterUninstall();
            setTimeout(function () {
                panel.remove();
            }, 310);
            $('body').css('overflow', 'inherit');
        }else{
            //移除的动画方向
            if (direction == 'top') {
                panel.find('.asideModuleContent').css('top', '100%');
            } else if (direction == 'left') {
                panel.find('.asideModuleContent').css('left', '100%');
            }

            // 当前页面是路由页面的话
            if (isUrl) {
                // 如果无历史记录，或者上一页是空白页，则回到首页；其他自然情况goback
                // if (document.referrer == "" || history.length == '1') {
                if (history.length == '1') {
                    location.hash = 'install';
                } else {
                    history.go(-1);
                }
            } else {
                $('body').css('overflow', 'inherit');

            }

            this.afterUninstall && this.afterUninstall();
            setTimeout(() => {
                panel.remove();
            }, 310)
        }
    }
}